
package com.probiz.estore.imports.handler.product;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.WholesalePrice;
import com.probiz.estore.imports.handler.ColumnHandler;
import com.probiz.estore.imports.handler.basic.ColumnBasicHandler;
import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;


public class WholesalePriceHandler extends ColumnBasicHandler implements ColumnHandler {
	private Logger logger = Logger.getLogger(WholesalePriceHandler.class);
	
	/**
	 * 分隔符，如"=",数量-数量=价格;
	 */
	private String toDelimiter;
	private String equalDelimiter;

	public String getToDelimiter() {
		return toDelimiter;
	}
	public void setToDelimiter(String toDelimiter) {
		this.toDelimiter = toDelimiter;
	}
	public String getEqualDelimiter() {
		return equalDelimiter;
	}
	public void setEqualDelimiter(String equalDelimiter) {
		this.equalDelimiter = equalDelimiter;
	}


	public void setProperty(ImportModel importModel,Column column) throws Exception {
		Product product = (Product)importModel.getTarget();
		try {
			int i=0;
			List<WholesalePrice> wholesalePriceList=new ArrayList<WholesalePrice>();
			for (String wholesalePriceString : column.getValues()) {
				wholesalePriceString=wholesalePriceString.trim();
				if(StringUtils.isEmpty(wholesalePriceString)){
					continue;
				}
				String obj[]=wholesalePriceString.split(equalDelimiter);
				BigDecimal price=new BigDecimal(obj[1]);
				String[] quantitiies= obj[0].split(toDelimiter);
				Integer minQuantity = Integer.valueOf(quantitiies[0]); 
				Integer maxQuantity = Integer.valueOf(quantitiies[1]); 
				WholesalePrice wholesalePrice = new WholesalePrice();
				wholesalePrice.setPrice(price);
				wholesalePrice.setMinQuantity(minQuantity);
				wholesalePrice.setMaxQuantity(maxQuantity);
				wholesalePrice.setSortOrder(i);
				wholesalePriceList.add(wholesalePrice);
				i++;
			}
			//start:条件检查
			Integer lastMaxQuantity = null; //上一个截止数量
			for(WholesalePrice wp: wholesalePriceList){
				if(wp.getMinQuantity()<0 && wp.getMaxQuantity()<0){
					//case:截止数量,起始数量小
					//设置结果
					importModel.setResult(ImportModel.RESULT_ERROR);
					String msg = "无法导入,因为【批发价】不符合格式要求，数量不能少于0，请检查";
					logger.error(msg);
					importModel.setMsg(msg);
					return;
				}
				if(wp.getMinQuantity().compareTo(wp.getMaxQuantity())>0){
					//case:截止数量<起始数量
					//设置结果
					importModel.setResult(ImportModel.RESULT_ERROR);
					String msg = "无法导入,因为【批发价】不符合格式要求，截止数量不能小于起始数量，请检查";
					logger.error(msg);
					importModel.setMsg(msg);
					return;
				}
				if(wp.getPrice().compareTo(new BigDecimal("0"))<0){
					//case:价格为负值
					//设置结果
					importModel.setResult(ImportModel.RESULT_ERROR);
					String msg = "无法导入,因为【批发价】不符合格式要求，价格不能为负值，请检查";
					logger.error(msg);
					importModel.setMsg(msg);
					return;
				}
				if(lastMaxQuantity!=null && wp.getMinQuantity()!=lastMaxQuantity+1){
					//case:起始数量设置错误
					//设置结果
					importModel.setResult(ImportModel.RESULT_ERROR);
					String msg = "无法导入,因为【批发价】不符合格式要求，起始数量设置错误，请检查";
					logger.error(msg);
					importModel.setMsg(msg);
					return;
				}
				lastMaxQuantity = wp.getMaxQuantity();
			}
			//end:  条件检查
			product.getImportProductSku().setImportWholesalePrices(wholesalePriceList);
			importModel.setResult(ImportModel.RESULT_SUCCESS);
			
		} catch (Exception e) {
			//设置结果
			e.printStackTrace();
			importModel.setResult(ImportModel.RESULT_ERROR);
			String msg = "无法导入,因为【批发价】不符合格式要求，请检查";
			logger.error(msg);
			importModel.setMsg(msg);
			return;
		}
	}
}
